﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using ZebraCoreLib.BasBiz;
using System.Reflection;
using System.Collections;
using ZebraCoreLib.Framework;

namespace Web.ZebraUIControls
{
    public enum ListInputType
    {
        None,
        StockStatusSelector = 100,
        TimeSelector = 101,
        OrderStatusSelector = 102
    }

    public abstract class ZebraListInputControl : ZebraInputControl
    {

        protected Dictionary<string, string> ListItem;
        public string ServiceMethod { get; set; }
        public ListInputType InputType { get; set; }

        protected string bizServiceClass
        {
            get
            {
                string bizClass = "";
                if (!string.IsNullOrEmpty(ServiceMethod))
                {

                    string[] service = ServiceMethod.Split('.');
                    for (int i = 0; i < service.Length - 1; i++)
                    {
                        bizClass += service[i] + ".";
                    }
                    if (bizClass.EndsWith("."))
                        bizClass = bizClass.Substring(0, bizClass.Length - 1);
                }
                return bizClass;
            }
        }

        protected string bizServiceMethod
        {
            get
            {
                string bizMethod = "";
                if (!string.IsNullOrEmpty(ServiceMethod))
                {

                    string[] service = ServiceMethod.Split('.');
                    bizMethod += service[service.Length - 1];

                }
                return bizMethod;
            }
        }

        protected IList InvokeServiceMethod(string serviceMethod)
        {
            Assembly assembly = Assembly.Load("ZebraCoreLib");
            Type bizServiceType = assembly.GetType(bizServiceClass, false, true);
            object bizService = Activator.CreateInstance(bizServiceType);
            MethodInfo method = bizServiceType.GetMethod(bizServiceMethod, BindingFlags.Public | BindingFlags.Instance);

            IList result = method.Invoke(bizService, null) as IList;
            return result;
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (InputType != ListInputType.None)
            {
                RenderCommondControl(writer);
            }
            else
            {
                RenderHeader(writer);
                RenderListItem(writer);
                RenderFooter(writer);
            }
        }

        public virtual void RenderCommondControl(HtmlTextWriter writer)
        {
            TemplatePart templatePart = (TemplatePart)Enum.Parse(typeof(TemplatePart), InputType.ToString());
            RenderControlFromTemplate(writer, templatePart);
        }

        protected virtual void RenderHeader(HtmlTextWriter writer)
        {
            RenderControlFromTemplate(writer, TemplatePart.Header);
        }
        protected virtual void RenderListItem(HtmlTextWriter writer)
        {
            IList bizServices = InvokeServiceMethod(ServiceMethod);
            string valueMember = "";
            string displayMember = "";
            foreach (var bizService in bizServices)
            {
                Hashtable item = new Hashtable();
                if (string.IsNullOrEmpty(valueMember))
                    valueMember = GetValueMember(ServiceMethod);

                if (string.IsNullOrEmpty(displayMember))
                    displayMember = GetDispalyMember(displayMember);

                string key = GetPropertyValue(bizService, valueMember);
                string value = GetPropertyValue(bizService, displayMember);
                item["Key"] = key;
                item["Value"] = value;

                if (Value == key)
                    item["Checked"] = "checked=checked";
                else
                    item["Checked"] = "";

                RenderControlFromTemplate(writer, TemplatePart.ListItem, null, item);
            }

        }

        protected string GetValueMember(object ServiceMethod)
        {
            string result = "";

            Assembly assembly = Assembly.Load("ZebraCoreLib");
            Type bizServiceType = assembly.GetType(bizServiceClass, false, true);
            object bizService = Activator.CreateInstance(bizServiceType);
            MethodInfo method = bizServiceType.GetMethod(bizServiceMethod, BindingFlags.Public | BindingFlags.Instance);

            var attributes = method.GetCustomAttributes(typeof(ZebraServiceMethodAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                var attr = attributes[0] as ZebraServiceMethodAttribute;
                result = attr.ValueMember;
            }
            return result;
        }
        protected string GetDispalyMember(object ServiceMethod)
        {
            string result = "";

            Assembly assembly = Assembly.Load("ZebraCoreLib");
            Type bizServiceType = assembly.GetType(bizServiceClass, false, true);
            object bizService = Activator.CreateInstance(bizServiceType);
            MethodInfo method = bizServiceType.GetMethod(bizServiceMethod, BindingFlags.Public | BindingFlags.Instance);

            var attributes = method.GetCustomAttributes(typeof(ZebraServiceMethodAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                var attr = attributes[0] as ZebraServiceMethodAttribute;
                result = attr.DisplayMember;
            }
            return result;
        }


        public string GetPropertyValue(object bizService, string property)
        {
            var p = bizService.GetType().GetProperty(property, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            return p.GetValue(bizService, null).ToString();
        }

        protected virtual void RenderFooter(HtmlTextWriter writer)
        {
            RenderControlFromTemplate(writer, TemplatePart.Footer);
        }


    }
}
